#!/bin/sh /etc/rc.common

START=30

cat /etc/config/cpufreq &> /dev/null
if [ $? == 1 ]; then
echo > /etc/config/cpufreq
fi

uci get cpufreq.@cpufreq[0] &> /dev/null
if [ $? == 1 ]; then
while uci -q delete cpufreq.@cpufreq[0]; do :; done
uci -q commit cpufreq
uci -q batch <<-EOF
 set cpufreq.cpufreq="cpufreq"
 set cpufreq.@cpufreq[0].min_freq="648000"
 set cpufreq.@cpufreq[0].max_freq="1008000"
 set cpufreq.@cpufreq[0].governor="ondemand"
 set cpufreq.@cpufreq[0].upthreshold="25"
 set cpufreq.@cpufreq[0].factor="10"
EOF
sleep 1
uci -q commit cpufreq
fi

min_freq=$(uci get cpufreq.@cpufreq[0].min_freq)
max_freq=$(uci get cpufreq.@cpufreq[0].max_freq)
governor=$(uci get cpufreq.@cpufreq[0].governor)
upthreshold=$(uci get cpufreq.@cpufreq[0].upthreshold)
factor=$(uci get cpufreq.@cpufreq[0].factor)

scaling_max_freq=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq)
scaling_min_freq=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq)
Filter="480000|648000|816000|1008000"

uci get cpufreq.@cpufreq[0].min_freq &> /dev/null
if [ $? == 1 ]; then  # Есть ли min_freq?
 uci set cpufreq.@cpufreq[0].min_freq="648000"
 uci commit cpufreq
elif echo $Filter | grep -qE $min_freq; then
:
else
 uci set cpufreq.@cpufreq[0].min_freq="648000"
 uci commit cpufreq
fi


start() {
cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies &> /dev/null
if [ $? != 1 ]; then

if [[ "$min_freq" != "$scaling_min_freq" || "$max_freq" != "$scaling_max_freq" ]]; then
 echo $governor > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
 echo $max_freq > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
 echo $min_freq > /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq
 echo $upthreshold > /sys/devices/system/cpu/cpufreq/ondemand/up_threshold
 echo $factor > /sys/devices/system/cpu/cpufreq/ondemand/sampling_down_factor
 echo 1 > /sys/devices/system/cpu/cpufreq/ondemand/io_is_busy
fi

fi

}

